using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WowDotNetAPI.Models;

namespace WowDotNetAPI.Test
{
    [TestClass]
    public class GuildTest : SilverlightTest
    {

        [TestMethod]
        [Asynchronous]
        public void Get_Simple_Guild_Immortality_From_Skullcrusher()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            Guild immortalityGuild = null;
            WowExplorer.GetGuild("skullcrusher", "immortality", GuildOptions.GetEverything, cb =>
                {
                    immortalityGuild = cb.Result;
                    done = true;
                });

            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsTrue(immortalityGuild.Realm.Equals("skullcrusher", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.AreEqual(UnitSide.ALLIANCE, immortalityGuild.Side));
            EnqueueCallback(() => Assert.IsTrue(immortalityGuild.Members.Any()));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Get_Valid_Human_Member_From_Immortality_Guild()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            Guild immortalityGuild = null;
            GuildMember briandek = null;
            WowExplorer.GetGuild("skullcrusher", "immortality", GuildOptions.GetEverything, cb =>
            {
                immortalityGuild = cb.Result;
                briandek = immortalityGuild.Members.Where(m => m.Character.Name.Equals("briandek", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                done = true;
            });

            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsTrue(briandek.Character.Name.Equals("briandek", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.AreEqual(85, briandek.Character.Level));
            EnqueueCallback(() => Assert.AreEqual(CharacterClass.WARRIOR, briandek.Character.@Class));
            EnqueueCallback(() => Assert.AreEqual(CharacterRace.HUMAN, briandek.Character.Race));
            EnqueueCallback(() => Assert.AreEqual(CharacterGender.MALE, briandek.Character.Gender));
            EnqueueTestComplete();
            //Assert.IsTrue(briandek.character.achievementPoints == 6895); achievements via guild api is broken ._.
        }


        [TestMethod]
        [Asynchronous]
        public void Get_Valid_Night_Elf_Member_From_Immortality_Guild()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            Guild immortalityGuild = null;
            GuildMember fleas = null;
            WowExplorer.GetGuild("skullcrusher", "immortality", GuildOptions.GetEverything, cb =>
            {
                immortalityGuild = cb.Result;
                fleas = immortalityGuild.Members.Where(m => m.Character.Name.Equals("fleas", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                done = true;
            });


            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsTrue(fleas.Character.Name.Equals("fleas", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.AreEqual(85, fleas.Character.Level));
            EnqueueCallback(() => Assert.AreEqual(CharacterClass.DRUID, fleas.Character.@Class));
            EnqueueCallback(() => Assert.AreEqual(CharacterRace.NIGHT_ELF, fleas.Character.Race));
            EnqueueCallback(() => Assert.AreEqual(CharacterGender.MALE, fleas.Character.Gender));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Get_Valid_Member_From_Another_Guild()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            Guild dvGuild = null;
            WowExplorer.GetGuild("laughing skull", "deus vox", GuildOptions.GetMembers | GuildOptions.GetAchievements, cb =>
            {
                dvGuild = cb.Result;
                done = true;
            });

            EnqueueConditional(() => done);

            EnqueueCallback(() => Assert.IsNotNull(dvGuild.Members));
            EnqueueCallback(() => Assert.IsNotNull(dvGuild.AchievementPoints));
            EnqueueCallback(() => Assert.IsTrue(dvGuild.Name.Equals("deus vox", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.IsTrue(dvGuild.Realm.Equals("laughing skull", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.IsTrue(dvGuild.Members.Any()));
            EnqueueCallback(() => Assert.AreEqual(UnitSide.ALLIANCE, dvGuild.Side));

            //need to find a valid horde character - or better mock this!
            //GuildMember ohnoes = dvGuild.Members.Where(m => m.Character.Name.Equals("ohnoes", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

            //Assert.IsTrue(ohnoes.Character.Name.Equals("ohnoes", StringComparison.InvariantCultureIgnoreCase));

            //Assert.AreEqual(85, ohnoes.Character.Level);
            //Assert.AreEqual(CharacterClass.ROGUE, ohnoes.Character.@Class);
            //Assert.AreEqual(CharacterRace.HUMAN, ohnoes.Character.Race);
            //Assert.AreEqual(CharacterGender.FEMALE, ohnoes.Character.Gender);
            EnqueueTestComplete();
        }


        [TestMethod]
        [Asynchronous]
        public void Get_Valid_Member_From_Horde_Guild()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            Guild rageGuild = null;
            WowExplorer.GetGuild("skullcrusher", "rage", GuildOptions.GetMembers , cb =>
            {
                rageGuild = cb.Result;
                done = true;
            });

            EnqueueConditional(() => done);

            EnqueueCallback(() => Assert.IsNotNull(rageGuild.Members));
            EnqueueCallback(() => Assert.IsNull(rageGuild.Achievements));

            EnqueueCallback(() => Assert.IsTrue(rageGuild.Name.Equals("rage", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.IsTrue(rageGuild.Realm.Equals("skullcrusher", StringComparison.InvariantCultureIgnoreCase)));

            EnqueueCallback(() => Assert.IsTrue(rageGuild.Members.Any()));

            EnqueueCallback(() => Assert.IsTrue(rageGuild.Side == UnitSide.HORDE));

            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Get_Guild_With_Only_Achievements()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);
            var WowExplorer2 = new WowExplorer(Region.US);

            Guild immortality = null;
            Guild immortalityGuild = null;
            WowExplorer.GetGuild("skullcrusher", "immortality", GuildOptions.GetAchievements, cb =>
              {
                  immortality = cb.Result;
                  WowExplorer2.GetGuild("skullcrusher", "immortality", GuildOptions.GetEverything, cbx =>
                  {
                      immortalityGuild = cbx.Result;
                      done = true;
                  });
              });


            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsNull(immortality.Members));
            EnqueueCallback(() => Assert.IsNotNull(immortality.Achievements));

            EnqueueCallback(() => Assert.IsTrue(immortalityGuild.Realm.Equals("skullcrusher", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.AreEqual(UnitSide.ALLIANCE, immortalityGuild.Side));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Get_Guild_With_Only_Members()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);
            var WowExplorer2 = new WowExplorer(Region.US);

            Guild immortality = null;
            Guild immortalityGuild = null;
            WowExplorer.GetGuild("skullcrusher", "immortality", GuildOptions.GetMembers, cb =>
            {
                immortality = cb.Result;
                WowExplorer2.GetGuild("skullcrusher", "immortality", GuildOptions.GetEverything, cbx =>
                {
                    immortalityGuild = cbx.Result;
                    done = true;
                });
            });

            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsNotNull(immortality.Members));
            EnqueueCallback(() => Assert.IsNull(immortality.Achievements));

            EnqueueCallback(() => Assert.IsTrue(immortalityGuild.Realm.Equals("skullcrusher", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.AreEqual(UnitSide.ALLIANCE, immortalityGuild.Side));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Get_Guild_With_Only_No_Options()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);
            var WowExplorer2 = new WowExplorer(Region.US);

            Guild immortality = null;
            Guild immortalityGuild = null;
            WowExplorer.GetGuild("skullcrusher", "immortality", GuildOptions.None, cb =>
            {
                immortality = cb.Result;
                WowExplorer2.GetGuild("skullcrusher", "immortality", GuildOptions.GetEverything, cbx =>
                {
                    immortalityGuild = cbx.Result;
                    done = true;
                });
            });

            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsNull(immortality.Members));
            EnqueueCallback(() => Assert.IsNull(immortality.Achievements));

            EnqueueCallback(() => Assert.IsTrue(immortalityGuild.Realm.Equals("skullcrusher", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.AreEqual(UnitSide.ALLIANCE, immortalityGuild.Side));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Get_Guild_With_Base_Method_Call()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);
            var WowExplorer2 = new WowExplorer(Region.US);

            Guild immortality = null;
            Guild immortalityGuild = null;  

            WowExplorer.GetGuild("skullcrusher", "immortality", cb =>
            {
                immortality = cb.Result;
                WowExplorer2.GetGuild("skullcrusher", "immortality", GuildOptions.GetEverything, cbx =>
                   {
                       immortalityGuild = cbx.Result;
                       done = true;
                   });
            });


            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsNull(immortality.Members));
            EnqueueCallback(() => Assert.IsNull(immortality.Achievements));

            EnqueueCallback(() => Assert.IsTrue(immortalityGuild.Realm.Equals("skullcrusher", StringComparison.InvariantCultureIgnoreCase)));
            EnqueueCallback(() => Assert.AreEqual(UnitSide.ALLIANCE, immortalityGuild.Side));
            EnqueueTestComplete();
        }
    }
}